<template>
    <a-form ref='formRef' :label-col='{span : 5}' :model='formData'>
        <a-form-item style='display:none' name='id'>
            <a-input type='hidden' v-model:value='formData.id' />
        </a-form-item>
        <a-form-item label='活动名称' name='name' :rules="[{required : true, message : '请填写名称'}]">
            <a-input type='text' v-model:value='formData.name' show-count :maxlength="50"  />
        </a-form-item>
        <a-form-item label='封面' name='cover' :rules="[{required : true, message : '请上传图片'}]">
            <a-upload list-type='picture-card' :max-count="1" v-model:file-list='coverList' :beforeUpload='beforeUpload' :customRequest='uploadCover' accept="image/png,image/jpeg,image/gif,image/webp">
                <template v-if='coverList.length == 0'>
                    <PlusOutlined />
                </template>
            </a-upload>
        </a-form-item>
        <a-form-item label='活动地点' name='address' :rules="[{required : true, message : '请填写地点'}]">
            <a-input type='text' v-model:value='formData.address' show-count :maxlength="100"  />
        </a-form-item>
        <a-form-item label='活动日期' name='dates' :rules="[{required : true, message : '请选择日期'}]">
            <a-range-picker v-model:value="formData.dates" valueFormat="X" />
        </a-form-item>
        <a-form-item label='报名日期' name='registrationDates' :rules="[{required : true, message : '请选择报名日期'}]">
            <a-range-picker v-model:value="formData.registrationDates" valueFormat="X" />
        </a-form-item>
        <!-- <a-form-item label='标签' name='labels'>
            <a-select v-model:value="formData.labels" :options="labels"  mode="multiple" style="width: 100%" :field-names="{ label: 'name', value: 'id'}" />
        </a-form-item> -->
        <a-form-item label='活动介绍' name='content'>
            <RichEditor v-model:excerpt="formData.excerpt" :excerpt-length="100" v-model="formData.content" :upload-api="uploadApi" />
        </a-form-item>
        <a-form-item label='排序' name='sort'>
            <a-input type='number' v-model:value='formData.sort' />
        </a-form-item>
        <!-- <a-form-item label='启用' name='enable'>
            <a-switch v-model:checked='formData.enable' checked-children='启用' un-checked-children='禁用'/>
        </a-form-item> -->
    </a-form>
</template>

<script lang="ts" setup>
    //import { industryLabelApi } from '@/api/system/Dictionary';
    import RichEditor from '@/components/RichEditor.vue'
        import { PlusOutlined } from '@ant-design/icons-vue';
        import { ref } from 'vue';
import { Modal } from "ant-design-vue";
import { uploadApi } from "@/api/Upload";
import { saveApi } from '@/api/Yunxin';

    const props = defineProps<{
        row? : any;
        category? : number
    }>()

    const coverList = ref<any[]>([])

    const labels = ref<any[]>([])

    const formRef = ref()

    
    const formData = ref<any>({
        id : 0,
        address: '',
        name : '',
        cover : '',
        labels: [],
        dates: [],
        registrationDates: [],
        excerpt : '',
        content: '',
        sort : 1,

    })

    if(props.row) {
        for(let k in props.row) {
            if(k in formData.value) {
                formData.value[k] = props.row[k]
            }
        }

        formData.value.dates = [props.row.startDate, props.row.startDate]
        formData.value.registrationDates = [props.row.registrationStartDate, props.row.registrationEndDate]

        if(props.row.cover) {
            coverList.value[0] = {
                status : 'done',
                url : import.meta.env.B_NET_STATIC_URL + props.row.cover
            }
        }

    }

    async function submit() {
        let params  = await formRef.value.validate()
        params.startDate = params.dates[0]
        params.endDate = params.dates[1]
        params.registrationStartDate = params.registrationDates[0]
        params.registrationEndDate = params.registrationDates[1]

        delete params.dates
        delete params.registrationDates


        params.excerpt = formData.value.excerpt


        if('sort' in params && params.sort === '') {
            params.sort = 0
        }


        return saveApi(params)
    }


    function uploadCover(e) {
        if(e.file.size > 1024 * 1024) {
            Modal.error({
                title : '图片不能大于1M'
            })
            return
        }



        uploadApi(e.file).then(res => {
            coverList.value[0].status = 'done'
            coverList.value[0].url = import.meta.env.B_NET_STATIC_URL + res.data
            formData.value.cover = res.data

            formRef.value.validateFields(['cover'])
        })
    }

    function beforeUpload(file:File, field:FormField) {
        return true
    }

    // industryLabelApi().then(data => {
    //     labels.value = data
    // })


    defineExpose({
        submit
    })
</script>